package com.heiko.myclienttest

import android.content.Context
import android.net.nsd.NsdManager
import android.net.nsd.NsdManager.DiscoveryListener
import android.net.nsd.NsdServiceInfo
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.heiko.myclienttest.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var mNsdManager: NsdManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        mNsdManager = getSystemService(Context.NSD_SERVICE) as NsdManager

        binding.btnDiscover.setOnClickListener {
            discoverServices()
        }
    }

    private fun discoverServices() {
        mNsdManager.discoverServices(
            "_http._tcp.",
            NsdManager.PROTOCOL_DNS_SD,
            object : DiscoveryListener {
                override fun onDiscoveryStarted(regType: String) {
                    // 当服务发现开始时调用
                }

                override fun onServiceFound(service: NsdServiceInfo) {
                    // 当发现一个服务时调用
                    mNsdManager.resolveService(service, object : NsdManager.ResolveListener {
                        override fun onResolveFailed(serviceInfo: NsdServiceInfo, errorCode: Int) {
                            // 解析服务失败时调用
                        }

                        override fun onServiceResolved(serviceInfo: NsdServiceInfo) {
                            // 服务解析成功时调用
                            val serviceName = serviceInfo.serviceName
                            val host = serviceInfo.host.hostAddress
                            val port = serviceInfo.port
                            // 在这里可以使用获取到的服务信息，例如连接到服务
                            Log.i("Heiko", "serviceName:$serviceName host:$host port:$port")
                            runOnUiThread {
                                Toast.makeText(this@MainActivity,"serviceName:$serviceName host:$host port:$port",Toast.LENGTH_SHORT).show()
                            }
                        }
                    })
                }

                override fun onServiceLost(service: NsdServiceInfo) {
                    // 当服务丢失时调用
                }

                override fun onDiscoveryStopped(serviceType: String) {
                    // 当服务发现停止时调用
                }

                override fun onStartDiscoveryFailed(serviceType: String, errorCode: Int) {
                    // 开始服务发现失败时调用
                }

                override fun onStopDiscoveryFailed(serviceType: String, errorCode: Int) {
                    // 停止服务发现失败时调用
                }
            })
    }
}